﻿Partial Class wuc_task
	Inherits System.Web.UI.UserControl

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			FillSupportingProjectData()
			FillSupportingPrioritiesData()
			bUpdate.Visible = False
			lOutcome.Text = ""
		End If

	End Sub

	Private Sub FillSupportingProjectData()
		Dim _bl As New scrumrBL
		Dim UserID As String

		UserID = Membership.GetUser.ProviderUserKey.ToString

		_bl.FillProjectListForSMUserID(ddlProjects, UserID)

		_bl = Nothing
	End Sub

	Private Sub FillSupportingPrioritiesData()
		Dim _bl As New scrumrBL

		_bl.FillPrioritiesList(ddlPriorities)

		_bl = Nothing
	End Sub

	Protected Sub ddlProjects_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlProjects.SelectedIndexChanged
		FillSupportingFeaturesData(ddlProjects.SelectedValue)
	End Sub

	Public Sub FillSupportingFeaturesData(ByVal projectID As String)
		Dim _bl As New scrumrBL

		_bl.FillFeaturesList(ddlFeatures, projectID)

		_bl = Nothing
	End Sub

	Protected Sub bSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bSave.Click
		Dim _t As New task
		Dim UserID As String

		lOutcome.Visible = True

		Try
			UserID = Membership.GetUser.ProviderUserKey.ToString

			_t.AddTask(ddlFeatures.SelectedValue, tName.Text, tDesc.Text, ddlPriorities.SelectedValue, tEstTime.Text, UserID)
			lOutcome.Text = String.Format("Feature {0} created!", _t.TaskId)
			bSave.Visible = "False"
			bNewTask.Visible = "True"
		Catch ex As Exception
			lOutcome.Text = ex.Message
		End Try

		_t = Nothing

	End Sub

	Protected Sub bNewTask_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bNewTask.Click
		Response.Redirect("TaskMaintenance.aspx")
	End Sub

	Public Sub UpdateTask(ByVal iProjectId As Integer, ByVal itaskID As Integer)

		bUpdate.Visible = True
		bSave.Visible = False
		lOutcome.Text = ""

		Dim _task As task

		Try
			_task = New task(itaskID)
		Catch ex As Exception
			lOutcome.Visible = True
			lOutcome.Text = ex.Message
		End Try

		hTaskId.Value = itaskID
		ddlProjects.Enabled = False
		ddlProjects.SelectedValue = iProjectId

		FillSupportingFeaturesData(iProjectId)
		ddlFeatures.Enabled = False
		ddlFeatures.SelectedValue = _task.FeatureID

		tName.Text = _task.TaskName
		tDesc.Text = _task.TaskDesc
		ddlPriorities.SelectedValue = _task.PriorityID
		tEstTime.Text = _task.EstimatedTime

		_task = Nothing

	End Sub

	Protected Sub bUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bUpdate.Click

		lOutcome.Visible = True

		Dim _task As New task()

		_task.TaskId = hTaskId.Value
		_task.TaskName = tName.Text
		_task.TaskDesc = tDesc.Text
		_task.PriorityID = ddlPriorities.SelectedValue
		_task.EstimatedTime = tEstTime.Text
		_task.UserID = Membership.GetUser.ProviderUserKey.ToString

		Try
			_task.UpdateTask()
			lOutcome.Text = "Task updated!"

		Catch ex As Exception
			lOutcome.Text = ex.Message
		End Try
		_task = Nothing

	End Sub

End Class
